import { Component, ViewChild } from '@angular/core';
import { Storage } from '@ionic/storage';
import { DomSanitizer } from '@angular/platform-browser';
import { Events, AlertController, App, ModalController, NavController, NavParams, ActionSheetController, Content } from 'ionic-angular';
import { IrpReportCollectListService } from './list.service';
import { Config } from '../../../providers/config';
import { IrpReportCollectDetailPage } from '../detail/detail.component';
import { Common } from '../../../../../providers/common';
import _ from 'lodash';

@Component({
  selector: 'page-irp-report-collect-list-list',
  templateUrl: './list.html',
  providers: [IrpReportCollectListService]
})
export class IrpReportCollectListPage {
	moa_page_name: any = "page-collect-list";
  moa_page_explain: any = "";

  public minnum:any;
  public maxnum:any;
  public isLoadMore = true;
  public reporttypeid:any = "";
  public ListData: Array<any>;

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public listService: IrpReportCollectListService,
    private navParams: NavParams,
    public events: Events,
    public storage: Storage,
    private sanitizer: DomSanitizer,
    public actionSheetCtrl: ActionSheetController,
    public common :Common
  ) {
    this.minnum = 1;
    this.maxnum = 10;
    this.getCollectList(1,10);
  }

  // 获取收藏列表
  getCollectList(minnum,maxnum){
    let that = this;
    this.listService.getCollectList(minnum,maxnum).subscribe(data=>{
      if (data) {
        // 如果加载第一页，则清空数据
        if(maxnum = 10){
          that.isLoadMore = true;
          that.ListData = [];
        }
        console.log("合并前：" + JSON.stringify(that.ListData));
        // 把现有数据和新加载的数据合并
        that.ListData = _.concat(that.ListData, data);
        console.log("合并后：" + JSON.stringify(that.ListData));
        // 如果返回数据少于请求页数，则不再加载更多
        if (data.length < 10) {
          that.isLoadMore = false;
        }
      } else {
        //this.ListData = [];
        that.isLoadMore = false;
      }
    })
  }
  //下拉刷新
  doRefresh(refresher){
    let that = this;
    this.minnum = 1;
    this.maxnum = 10;
    this.isLoadMore = true;
    this.getCollectList(this.minnum,this.maxnum);
    if (refresher) {
      setTimeout(() => { refresher.complete(); }, 1000);
    }
  }
  //上拉加载
  doInfinite(infiniteScroll){
    let that = this;
    this.minnum = this.minnum + 10;
    this.maxnum = this.maxnum + 10;
    if (that.isLoadMore) {
      that.getCollectList(that.minnum,that.maxnum);
      setTimeout(() => { infiniteScroll.complete(); }, 1000);
    }else{
      setTimeout(() => { infiniteScroll.complete(); }, 1000);
    }
  }
  // 进入详情
  getCollectDetail(item){
    this.navCtrl.push(IrpReportCollectDetailPage, item);
  }
}